package com.meida.module.bus.provider.utils;

import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

@Slf4j
public class MyDateUtils extends DateUtils {

    public static Date addMin(int min) {
        Date date = new Date();//取时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.MINUTE, min);
        date = calendar.getTime();
        return date;
    }

    public static Date addMin(Date date, int min) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.MINUTE, min);
        date = calendar.getTime();
        return date;
    }

    public static Date addSecond(Date date, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.SECOND, second);
        date = calendar.getTime();
        return date;
    }

    public static Date addHours(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.HOUR, hours);
        date = calendar.getTime();
        return date;
    }

    /**
     * @param date1 小
     * @param date2 大
     * @return
     */
    public static Long getDistanceSeconds(Date date1, Date date2) {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        return (time2 - time1) / 1000L;
    }

    /**
     * @param date1 小
     * @param date2 大
     * @return
     */
    public static Long getDistanceMinutes(Date date1, Date date2) {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        return (time2 - time1) / 1000L / 60L;
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 将本地时间, 转换成目标时区的时间
     *
     * @param sourceDate
     * @param targetZoneId
     * @return
     */
    public static Date convertTimezone(Date sourceDate, String targetZoneId) {
        return convertTimezone(sourceDate, TimeZone.getTimeZone(targetZoneId));
    }

    public static Date convertTimezone(Date sourceDate, String sourceZoneId, String targetZoneId) {
        TimeZone sourceTimeZone = TimeZone.getTimeZone(sourceZoneId);
        TimeZone targetTimeZone = TimeZone.getTimeZone(targetZoneId);
        return convertTimezone(sourceDate, sourceTimeZone, targetTimeZone);
    }

    public static String convertTimezone(String sourceDate, String targetZoneId) {
        Date date = strToDate(sourceDate, "yyyy-MM-dd HH:mm:ss");
        TimeZone sourceTimeZone = TimeZone.getTimeZone("");
        TimeZone targetTimeZone = TimeZone.getTimeZone(targetZoneId);
        Date timezoneDate = convertTimezone(date, sourceTimeZone, targetTimeZone);
        return DateUtils.formatDate(timezoneDate, "yyyy-MM-dd HH:mm:ss");
    }
    public static String convertTimezone(String sourceDate, String sourceZoneId, String targetZoneId) {
        Date date = strToDate(sourceDate, "yyyy-MM-dd HH:mm:ss");
        TimeZone sourceTimeZone = TimeZone.getTimeZone(sourceZoneId);
        TimeZone targetTimeZone = TimeZone.getTimeZone(targetZoneId);
        Date timezoneDate = convertTimezone(date, sourceTimeZone, targetTimeZone);
        return DateUtils.formatDate(timezoneDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将本地时间,转换成对应时区的时间
     *
     * @param localDate
     * @param targetTimezone 转换成目标时区所在的时间
     * @return
     */
    public static Date convertTimezone(Date localDate, TimeZone targetTimezone) {
        return convertTimezone(localDate, TimeZone.getDefault(), targetTimezone);
    }


    /**
     * 将sourceDate转换成指定时区的时间
     *
     * @param sourceDate
     * @param sourceTimezone sourceDate所在的时区
     * @param targetTimezone 转化成目标时间所在的时区
     * @return
     */
    public static Date convertTimezone(Date sourceDate, TimeZone sourceTimezone, TimeZone targetTimezone) {
        // date.getTime() 为时间戳, 为格林尼治到系统现在的时间差,世界各个地方获取的时间戳是一样的
        // 格式化输出时,因为设置了不同的时区,所以输出不一样
        Calendar calendar = Calendar.getInstance();
        long sourceTime = sourceDate.getTime();
        calendar.setTimeZone(sourceTimezone);
        // 设置之后,calendar会计算各种filed对应的值,并保存
        calendar.setTimeInMillis(sourceTime);
        //获取源时区的到UTC的时区差
        int sourceZoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        calendar.setTimeZone(targetTimezone);
        calendar.setTimeInMillis(sourceTime);
        int targetZoneOffset = calendar.get(Calendar.ZONE_OFFSET);
        //int targetDaylightOffset = calendar.get(Calendar.DST_OFFSET); // 夏令时
        long targetTime = sourceTime + (targetZoneOffset ) - sourceZoneOffset;
        return new Date(targetTime);

    }

    public static String formatFromTodayCn(Date createAt, String targetTimezone) {
        log.info("targetTimezone:" + targetTimezone);
        String interval = null;
        if (createAt == null) {
            return "";
        } else {
            //服务器是GMT时间
            long currentTimeMillis = System.currentTimeMillis();
            log.info("currentTimeMillis:" + currentTimeMillis);
            //先转成默认的GMT
            Date date = MyDateUtils.convertTimezone(createAt, "");
            long millisecond = currentTimeMillis - date.getTime();
            long second = millisecond / 1000L;
            if (second <= 0L) {
                second = 0L;
            }

            if (second == 0L) {
                interval = "刚刚";
            } else if (second < 30L) {
                interval = second + "秒以前";
            } else if (second >= 30L && second < 60L) {
                interval = "半分钟前";
            } else {
                long day;
                if (second >= 60L && second < 3600L) {
                    day = second / 60L;
                    interval = day + "分钟前";
                } else if (second >= 3600L && second < 86400L) {
                    day = second / 60L / 60L;
                    interval = day + "小时前";
                } else if (second >= 86400L && second <= 172800L) {
                    interval = "昨天";
                } else if (second >= 172800L && second <= 604800L) {
                    day = second / 60L / 60L / 24L;
                    interval = day + "天前";
                } else if (second > 604800L && second <= 2592000L) {
                    day = second / 60L / 60L / 24L;
                    interval = day + "天前";
                } else if (second <= 31536000L && second > 2592000L) {
                    createAt = MyDateUtils.convertTimezone(createAt, targetTimezone);
                    interval = formatDate(createAt, "MM-dd");
                } else if (second >= 31536000L) {
                    if (FlymeUtils.isNotEmpty(targetTimezone)) {
                        createAt = MyDateUtils.convertTimezone(createAt, targetTimezone);
                    }
                    interval = formatDate(createAt, "yyyy-MM-dd");
                } else {
                    interval = "0";
                }
            }
            return interval;
        }
    }

    public static void main(String[] args) {
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        System.out.println(timeZone);
        String dateStr = "2021-11-29 23:15:00";
        Date date = DateUtils.strToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
        Date date2 = convertTimezone(date,  "Asia/Shanghai","UTC");
        System.out.println(date2);



    }
}
